

/*
*
*   方法用于项目的权限控制，
*   包含有[data-permission] 属性的按钮为设有权限控制的按钮
*
* */


import store from '../stores/stores.js'
const renderPermission = {
    //渲染入口
    render: function () {
        let _this = this;
        let permissionList = store.getters.getSystemPermissions;
        let user = store.getters.getUser;
        // enableFlag:0 可用 1 禁用
        // visibleFlag 0:可见 1隐藏
        let domList = document.querySelectorAll("[data-permission]");
        if (!user || JSON.stringify(user) === "{}" || permissionList.length === 0) {
            _this.renderOfDefault(domList); //没有操作权限
        } else {
            _this.renderByPermission(permissionList, domList);
        }
    },

    //按照权限列表进行渲染
    renderByPermission: function (permissionList, domList) {
        for (let i = 0; i < domList.length; i++) {
            let dom = domList[i];
            let itemValue = dom.getAttribute("data-permission");
            for (let j = 0; j < permissionList.length; j++) {
                let tempPermission = permissionList[j];
                if (itemValue === tempPermission.code) {
                    if (tempPermission.visibleFlag == 0) {
                        //可见
                        if (tempPermission.enableFlag == 1) { //禁用
                            dom.setAttribute(
                                "title",
                                tempPermission.name + "功能,您没有操作权限,请联系管理员进行分配权限!"
                            );

                            dom.style.cssText = "display:inline; filter: grayscale(1);";
                            dom.setAttribute("disabled", "false");
                        }
                    } else if (tempPermission.visibleFlag == 1) {
                        //不可见
                        dom.style.cssText = "display:none";
                    }
                    break;
                }
            }
        }


    },

    // 将相应dom节点有data-permission标识的设置无操作权限渲染
    renderOfDefault: function (domList) {
        for (let i = 0; i < domList.length; i++) {
            let dom = domList[i];
            let title = dom.getAttribute("title");
            dom.setAttribute(
                "title",
                title + "功能,您没有操作权限,请联系管理员进行分配权限!"
            );

            dom.style.cssText = "display:inline; filter: grayscale(1);";
            dom.setAttribute("disabled", "false");
        }
    }
}
export default renderPermission
